package org.jeecgframework.web.system.controller.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.ZyCommonService;
import org.jeecgframework.web.system.pojo.base.CbsInspectionEvaluateEntity;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyContactsEntity;
import org.jeecgframework.web.system.pojo.base.ZyEquipmentSpareEntity;
import org.jeecgframework.web.system.pojo.base.ZyInspectionPlanEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Scope("prototype")
@Controller
@RequestMapping("/cbsWxUserCenterController")
public class CbsWxUserCenterController {

	@Autowired
	private SystemService systemService;

	@Autowired
	private ZyCommonService zyCommonService;

	/**
	 * 我的设备列表页
	 */
	@RequestMapping(params = "userEquipList")
	public ModelAndView userEquipList( HttpServletRequest request) {
//		TSUser user = ResourceUtil.getSessionUserName();
		TSUser user = genUser(request);
		List<ZyContactsEntity> contactList = systemService.findHql("from ZyContactsEntity where tsBaseUser.id = ?", user.getId());
		List<ZyEquipmentSpareEntity> esList = new ArrayList<>();
		if(contactList.size() > 0 && StringUtil.isNotEmpty(contactList.get(0).getBelongCinema())){
			esList = systemService.findHql("from ZyEquipmentSpareEntity where isSpare = false and zyCinema.id = ?", contactList.get(0).getBelongCinema().getId());
		    for(ZyEquipmentSpareEntity spare : esList){
                if(spare.getRepairEndDate().after(new Date())){
                    spare.setIsServicing("0");
                }else if(spare.getRepairEndDate().before(new Date())){
                    spare.setIsServicing("1");;
                }
            }
        }
		request.setAttribute("user", user);
		request.setAttribute("equip", esList);
		request.setAttribute("size", esList.size());
		return new ModelAndView("com/wx/personal_center/my_equipment_spare/wx_user_equip_list");
	}

	/**
	 * 我的设备详情页
	 */
	@RequestMapping(params = "userEquipListCon")
	public ModelAndView userEquipListCon(String id, HttpServletRequest request) {
		try {
			ZyEquipmentSpareEntity equipment = systemService.getEntity(ZyEquipmentSpareEntity.class, id);
			List<ZyInspectionPlanEntity> ipList = systemService.findHql("from ZyInspectionPlanEntity where state = ? and equipmentSpare.id = ? order by actualDate desc", StaticValue.DIC_PLAN_STATE_INSPECTION_COMPLETE,equipment.getId());
			if(ipList.size() > 0){
				request.setAttribute("actualDate", DateUtils.date2Str(ipList.get(0).getActualDate(), DateUtils.date_sdf));
				List<CbsInspectionEvaluateEntity> ipeList = systemService.findHql("from CbsInspectionEvaluateEntity where inspection.id = ?", ipList.get(0).getId());
				if(ipeList.size()>0){
					request.setAttribute("lastEvaluate", ipeList.get(0).getLastEvaluate());
					request.setAttribute("lastEvaluateDate", DateUtils.formatDate(ipeList.get(0).getInspection().getEndTime(),"yyyy-MM-dd"));
				}
			}
            if(equipment.getRepairEndDate().after(new Date())){
                equipment.setIsServicing("0");
            }else if(equipment.getRepairEndDate().before(new Date())){
                equipment.setIsServicing("1");
            }
			if(StringUtil.isNotEmpty(equipment.getRepairEndDate())){
				Date repairDate = equipment.getRepairEndDate();
				//保期截至时间
				String format = DateUtils.formatDate(repairDate, "yyyy-MM-dd");
				request.setAttribute("repairEndDate", format);
				Integer ceil = 0;
				long repairDateTime = repairDate.getTime();
				Date date = new Date();
				long time = date.getTime();
				long dateTime = repairDateTime - time;
				ceil = (int) Math.ceil(dateTime / 1000 / 60 / 60 / 24);
				request.setAttribute("ceil", ceil);
			}
			request.setAttribute("equipment", equipment);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("com/wx/personal_center/my_equipment_spare/wx_user_equip_list_con");
	}

	public TSUser genUser(HttpServletRequest request){
		TSUser u = ResourceUtil.getSessionUserName();
		if(null == u){
			String openId = oConvertUtils.getString(request.getSession().getAttribute("openId")) ;
			u = zyCommonService.findUniqueByProperty(TSUser.class,"openId", openId);
		}
		return u;
	}

}
